package eventplanner.ejb.adders;

import eventplanner.entities.Event;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Stateless
public class GroupAdder {

	@PersistenceContext
	private EntityManager em;

	public List<Event> getEventsWithOrganizer(Set<String> groups) {
		List<Event> allEvents = new ArrayList<>();

		for (String groep : groups) {
			TypedQuery<Event> query = em.createQuery("select e from Event e where e.organizer = ?1", Event.class);
			query.setParameter(1, groep);
			allEvents.addAll(query.getResultList());
		}

		return allEvents;
	}

	public List<Event> getEventsWithTarget(Set<String> targets) {
		List<Event> allEvents = new ArrayList<>();

		for (String target : targets) {
			TypedQuery<Event> query = em.createQuery("select e from Event e where e.doelgroep = ?1", Event.class);
			query.setParameter(1, target);
			allEvents.addAll(query.getResultList());
		}

		return allEvents;
	}
}
